﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdImportMesh.h"
#include "CommandIds.h"
#include "FilePath.h"
#include "GbmpFileSystem.h"
#include "CommandRegister.h"
#include "IMesh.h"
#include "IDirectShape.h"
#include "UiDocumentViewUtils.h"
#include "IUserTransaction.h"
#include "GcmpBuiltInCategoryUniIdentities.h"
#include "IGraphicsMeshBody.h"
#include "UiCommonDialog.h"
#include "IGraphicsStyle.h"
#include "IGraphicsStyleData.h"
#include "IGraphicsStyleManager.h"
#include "IGraphicsNodeStyleAndMaterialOverride.h"
#include "IAction.h"
#include "IDocument.h"
#include "IGraphicsElementShape.h"
#include "IGraphicsNodeStyleAndMaterialCustomizer.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 


using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdImportMesh)

CmdImportMesh::CmdImportMesh() : CommandBase(ID_CMD_IMPORT_MESH, true)
{

}

CmdImportMesh::~CmdImportMesh()
{

}

bool CmdImportMesh::IsEnabled() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

bool CmdImportMesh::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

gcmp::OwnerPtr<gcmp::IAction> CmdImportMesh::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"pDocument为空", L"GDMP", L"2023-09-30");

    //1.选择mesh数据
    std::wstring filePath = UiCommonDialog::ShowOpenFileDialog(GBMP_TR(L"打开obj文件"), gcmp::FileSystem::GetWorkingDirPath(), L"*.obj");
    if (filePath.empty())
    {
        return nullptr;
    }

    //2.读取mesh数据，并生成mesh
    OwnerPtr<IMesh> opMesh=IMesh::ReadObjTriMesh(filePath);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opMesh, L"读取Mesh失败", L"GDMP", L"2023-09-30");
    Vector3d ptCenter = opMesh->GetBox().GetCenter();
    constexpr int iMaxBoxHalfWidth = 500000;
    if ( ( fabs(ptCenter.X()) > iMaxBoxHalfWidth ) || ( fabs(ptCenter.Y()) > iMaxBoxHalfWidth ) )
    {
        opMesh->Translate(-ptCenter);
    }

    //3.创建DirectShape来显示mesh
    OwnerPtr<IUserTransaction> opUserTransaction = IUserTransaction::Create(pDocument, GBMP_TR(L"创建Mesh"));   
    gcmp::IDirectShape* pDirectShape = gcmp::IDirectShape::Create(pDocument, BuiltInCategoryUniIdentities::BICU_FORM);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDirectShape, L"pDirectShape为空", L"GDMP", L"2023-09-30");
    gcmp::OwnerPtr<IGraphicsElementShape> opShape = IGraphicsElementShape::Create(gcmp::GraphicsRenderLayer::Model);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opShape, L"opShape为空", L"GDMP", L"2023-09-30");
    opShape->AddChild(IGraphicsMeshBody::Create(TransferOwnership(opMesh)));
    pDirectShape->SetGraphicsElementShape(TransferOwnership(opShape));
    //4.设置DirectShape图形样式
    IGraphicsStyle* pGraphicsStyle = IGraphicsStyle::Create(pDocument, gcmp::ElementCreationOptions::Normal);
    OwnerPtr<IGraphicsStyleData> opGraphicsStyleData = IGraphicsStyleData::Create();
    Color color = Color::Create();
    color.SetBlue(rand() % 255);
    color.SetGreen(rand() % 255);
    color.SetRed(rand() % 255);
    opGraphicsStyleData->SetColor(color);
    pGraphicsStyle->SetGraphicsStyleData(*opGraphicsStyleData);
    gcmp::IGraphicsNodeStyleAndMaterialOverride* pIGraphicsNodeStyleAndMaterialOverride = pDirectShape->GetOwnerElement()->GetShapeGraphicsNodeStyleAndMaterialOverrideComponentFW();
    if (pIGraphicsNodeStyleAndMaterialOverride)
    {
        pIGraphicsNodeStyleAndMaterialOverride->SetGraphicsNodeStyleAndMaterialCustomizer(nullptr);
        pIGraphicsNodeStyleAndMaterialOverride->OverrideGraphicsStyle(pGraphicsStyle->GetElementId());
    }
    opUserTransaction->Commit();

    return nullptr;
}